package easier.recycler;

import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView.Adapter;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import easier.recycler.FastAdapter.FastHolder;

/**
 * 一个快速创建的adapter
 */
public class FastAdapter extends Adapter< FastHolder > {

    private final FastAdapterFactory mFactory;

    public FastAdapter( FastAdapterFactory factory ) {

        mFactory = factory;
    }

    /**
     * 辅助类,辅助创建Adapter,对用户暴露需要实现的方法
     */
    public static abstract class FastAdapterFactory {

        public abstract int getItemCount();

        protected abstract View onCreateView( @NonNull ViewGroup parent );

        protected void onBind( View root, int position ) {}

        protected void onUnbind( View root, int position ) {}
    }

    @NonNull
    @Override
    public FastHolder onCreateViewHolder( @NonNull ViewGroup parent, int viewType ) {

        return new FastHolder( mFactory.onCreateView( parent ) );
    }

    @Override
    public void onBindViewHolder( @NonNull FastHolder holder, int position ) {

        mFactory.onBind( holder.itemView, position );
    }

    @Override
    public void onViewRecycled( @NonNull FastHolder holder ) {

        mFactory.onUnbind( holder.itemView, holder.getAdapterPosition() );
        super.onViewRecycled( holder );
    }

    @Override
    public int getItemCount() {

        return mFactory.getItemCount();
    }

    /**
     * 辅助创建
     */
    public static class FastHolder extends ViewHolder {

        public FastHolder( @NonNull View itemView ) {

            super( itemView );
        }
    }
}
